package com.woniuxy.stage3demo.controller.controllercai;


import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.caicai.UserBean;
import com.woniuxy.stage3demo.bean.caicai.UserInfoBean;
import com.woniuxy.stage3demo.bean.caicai.dto.UserBeanDTO;
import com.woniuxy.stage3demo.bean.caicai.vo.UserVO;
import com.woniuxy.stage3demo.bean.hpxbean.OrderBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.servicecai.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userServiceImpl;


    /**
     * 用户列表基本信息的展示
     * @param userBean
     * @return
     */
    //@GetMapping("/list")
    @RequestMapping(value = "/list",produces = {"application/json;charset=utf-8"})
    public TableResult<UserVO> findBySerach(PageRequest request, UserBeanDTO userBean,Integer totalOrder){
        PageInfo<UserVO> pageInfo = userServiceImpl.findBySearch(request,userBean);

        return new TableResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     *用户详情查询
     */
        @RequestMapping(value = "/findinforid",produces = {"application/json;charset=utf-8"})
        public UserBean findUserById(long id){
            UserBean userById = userServiceImpl.findUserById(id);
            return userById;
    }
    @RequestMapping(value = "/findinforid2",produces = {"application/json;charset=utf-8"})
    public UserBean findUserById2(long id){
        UserBean userById = userServiceImpl.findUserById2(id);
        return userById;
    }
    @RequestMapping(value = "/findinforid3",produces = {"application/json;charset=utf-8"})
    public UserBean findUserById3(long id){
        UserBean userById = userServiceImpl.findUserById3(id);
        return userById;
    }
    /**
     * 用户的状态修改
     */
    //批量更改渠道状态
    @PutMapping(value = "/mulOpt/{ids}/{selOpt}",produces = {"application/json;charset=utf-8"})
    public ResultMsg UpdateUserStatus(@PathVariable("ids") Long ids[],@PathVariable("selOpt") int selOpt){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        for (Long id:ids) {
            userServiceImpl.updateUserStatus(selOpt,id);
        }
        return res;
    }
    //更改用户状态
    @PutMapping(value = "/status/{id}/{status}",produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        int opt;
        if(id != null && status != null) {
            if (status==1){
                opt=1;
            }else {
                opt=0;
            }
            userServiceImpl.updateUserStatus(opt,id);

            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    /**
     *用户删除
     */
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delUser(@PathVariable("id")Long id){
        ResultMsg res= new ResultMsg(200,"操作成功",null);
        if(id != null) {
            userServiceImpl.delUser(id.intValue());
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    /**
     * 查询用户，配偶，担保人真实信息
     * @param
     * @return
     */
    @RequestMapping(value = "/detail",produces = {"application/json;charset=utf-8"})
    public UserInfoBean findUserInfo(Long id){

        UserInfoBean userInfo = userServiceImpl.findUserInfo(id.intValue());
        return userInfo;
    }



    @RequestMapping(value = "/userorder",produces = {"application/json;charset=utf-8"})
    public List<OrderBean> findOrderByUID(Long id){

    List<OrderBean> orderByUID = userServiceImpl.findOrderByUID(id);
    return orderByUID;
}

    /**
     * 根据id批量删除用户
     * @param
     * @return
     */
    @PutMapping(value = "/delmore/{ids}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delMoreUser(@PathVariable("ids") Long ids[]){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        for (Long id:ids) {
            userServiceImpl.delMoreUser(id);
        }
        return res;
    }
}
